package se.perp.c4.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import se.perp.c4.model.Actor;

public class ActorViewer extends JPanel {

    private Actor viewedActor;
    private Image portrait = null;
    private final JTextPaneAA textPane = new JTextPaneAA();
    private final Color lightBgColor = ColorSet.LIGHTREGION_BG;
    private final Color darkBgColor = ColorSet.DARKREGION_BG;
    private final Color lightTextColor = Color.BLACK;
    private final Color darkTextColor = Color.WHITE;
    private Color attackValueColor;
    private final Color lightAttackValueColor = ColorSet.DARKREGION_BG;
    private final Color darkAttackValueColor = ColorSet.LIGHTREGION_BG;
    private final Font attackValueFont;
    private Font nameFont;
    private Font textFont;
    private Color nameTextColor;
    private final int MARGIN = 10;
    private final int W = 200;
    private final int H = 220;
    private final int IMAGE_Y = 35;
    private final int TEXTBOX_Y = IMAGE_Y + 100;
    private final int TEXTBOX_H = H - TEXTBOX_Y - 5;
    private final Color defaultBGColor = Color.DARK_GRAY;

    public ActorViewer() {
        attackValueFont = FontSet.ACTORVIEWER_ATK_VALUE;
        nameFont = FontSet.ACTORVIEWER_NAME;
        textFont = FontSet.ACTORVIEWER_TEXT;

        setLayout(null);
        setBackground(defaultBGColor);
        setBorder(BorderFactory.createRaisedBevelBorder());

        textPane.setBounds(MARGIN, TEXTBOX_Y, W - 2 * MARGIN, TEXTBOX_H);
        textPane.setEditable(false);
        textPane.setBackground(null);
        textPane.setFont(textFont);
        textPane.setSelectionColor(null);
        textPane.setHighlighter(null);

        add(textPane);
        setSize(W, H);
        setPreferredSize(new Dimension(W, H));

        setVisible(true);
    }

    /**
     * Sets the viewed actor to be null if the supplied argument is the
     * currently viewed actor.
     * To be used when an actor is killed - if it was the currently viewed one,
     * it disappears from the ActorViewer as well.
     */
    public boolean nullifyIfCurrent(Actor actor) {
        if (actor == viewedActor) {
            setActor(null);
            return true;
        }
        return false;
    }

    public void setActor(Actor actor) {
        viewedActor = actor;
        if (actor != null) {
            portrait = Portraits.get(actor.who);
            textPane.setText(actor.description);
            if (actor.isLight()) {
                setBackground(lightBgColor);
                textPane.setForeground(lightTextColor);
                nameTextColor = lightTextColor;
                attackValueColor = lightAttackValueColor;
            } else {
                setBackground(darkBgColor);
                textPane.setForeground(darkTextColor);
                nameTextColor = darkTextColor;
                attackValueColor = darkAttackValueColor;
            }
        } else {
            portrait = null;
            textPane.setText(null);
        }
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        Font savedFont = g2.getFont();
        if (viewedActor == null) {
            setBackground(defaultBGColor);
            super.paintComponent(g);
            g2.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
            g2.setColor(Color.WHITE);

            String s = "Click a piece for information.";
            int strW = g2.getFontMetrics().stringWidth(s);
            int strH = g2.getFontMetrics().getHeight();
            g2.drawString(s, W / 2 - strW / 2, H / 2 + strH / 2);
        } else {
            super.paintComponent(g);

            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            g2.setFont(nameFont);
            g2.setColor(nameTextColor);
            int y = IMAGE_Y - g2.getFontMetrics().getDescent();
            g2.drawString(viewedActor.name, MARGIN, y);

            g2.drawImage(portrait, MARGIN, IMAGE_Y, null);

            g.setColor(attackValueColor);
            g2.setFont(attackValueFont);

            String s = Integer.toString(viewedActor.attackValue);
            int strW = g2.getFontMetrics().stringWidth(s);
            int strH = g2.getFontMetrics().getHeight();
            int avx = -10 + MARGIN + (int) (1.5 * portrait.getWidth(null)) - strW / 2;
            int avy = IMAGE_Y + portrait.getHeight(null) / 2 + strH / 4;
            g2.drawString(s, avx, avy);
        }
        g2.setFont(savedFont);

    }
}
